package com.gp.geits.controller;

import java.util.Arrays;
import java.util.Map;

import com.gp.geits.Enum.ResultCodeEnum;
import com.gp.geits.exception.GETSException;
import com.gp.geits.utils.JwtInfo;
import com.gp.geits.utils.JwtUtils;
import com.gp.geits.utils.PageUtils;
import com.gp.geits.utils.R;
import com.gp.geits.vo.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gp.geits.entity.UserEntity;
import com.gp.geits.service.UserService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author lzf
 * @date 2022-03-09 15:36:11
 */
@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 学生注册
     */
    @PostMapping("/registerByStu")
    public R registerByStu(@RequestBody UserEntity userEntity){
        userService.registerByStu(userEntity);
        return R.ok().message("注册成功");
    }

    /**
     * 企业注册
     */
    @PostMapping("/registerByCom")
    public R registerByCom(@RequestBody RegisterByComVo vo){
        userService.RegisterByComVo(vo);
        return R.ok().message("注册成功");
    }

    /**
     * 学生登录
     */
    @PostMapping("/loginByStu")
    public R loginByStu(@RequestBody loginByStuVo loginVo){
        String token = userService.loginByStu(loginVo);
        return R.ok().message("登录成功").data("token",token);
    }

    /**
     * 企业登录
     */
    @PostMapping("/loginByCom")
    public R loginByCom(@RequestBody loginByComVo loginVo){
        String token = userService.loginByCom(loginVo);
        return R.ok().message("登录成功").data("token",token);
    }

    /**
     * 解析header中的token并返回
     */
    @GetMapping("/get-login-info")
    public R getLoginInfo(HttpServletRequest request){
        try {
            JwtInfo userInfo = JwtUtils.getMemberIdByJwtToken(request);
            return R.ok().data("userInfo",userInfo);
        }catch (Exception e){
            log.error("解析用户信息失败：" + e.getMessage());
            throw new GETSException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }

    /**
     * 修改个人基本信息
     */
    @PostMapping("/updatePersonalInformation")
    public R updatePersonalInformation(@RequestBody UpdatePersonalInformationVo vo,
                                       HttpServletRequest request){
        userService.updatePersonalInformation(vo,request);
        return R.ok().message("修改成功");
    }

    /**
     * 修改密码
     */
    @PostMapping("/passwordChange")
    public R passwordChange(@RequestBody PasswordChangeVo vo,
                            HttpServletRequest request){
        userService.passwordChange(vo,request);
        return R.ok().message("修改成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().data("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{phone}")
    public R info(@PathVariable("phone") Integer phone){
		UserEntity user = userService.getById(phone);

        return R.ok().data("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] phones){
		userService.removeByIds(Arrays.asList(phones));

        return R.ok();
    }

}
